package org.grottarossa.domain;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Serialized;

import org.apache.solr.common.SolrInputField;
import org.grottarossa.query.docs.RecordSolrDoc;

@PersistenceCapable(identityType = IdentityType.DATASTORE, detachable = "true")
@FetchGroup(name = "caseF", members = { @Persistent(name = "fields") })
public class Record extends RecordSolrDoc
{
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
	@Serialized
	private String id;

	@Persistent
	private long createTime = System.currentTimeMillis();

	@Element(embedded="false")
	private Collection<Field> fields;


	public void addField(Field field)
	{
		super.addField(field.getName(),field.getValue());
	}
	
	
	public long getCreateTime() {
	return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param fields the fields to set
	 */
	public void setFields(Set<Field> fields) {
		this.fields = fields;
		
		Iterator<Field> iter = fields.iterator();
		while(iter.hasNext())
		{
			Field field = iter.next();
			//super.addField("name",field.getName());
			super.addField("features",field.getValue());
			
		}
	}

	/**
	 * @return the fields
	 */
	public Collection<Field> getFields() {
		return fields;
	}

	@Override
	public boolean equals(Object obj) {
	
		return compare(obj);
	}
	
	@Override
	public void update(Object obj) 
	{	
		update(obj, Record.class);	 
	}

	@Override
	public Object cloneObj() 
	{		
		return cloneObj(Record.class);
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}
}